<template>
	<view class="container">
		<view class="addressbox">
			<view class="address">
				<text>{{address.province}}{{address.city}}{{address.county}}</text>
			</view>
			<view class="name" @click="jumpadd">
				<view class="left">
					<view class="top">
						<view>
							{{address.addressDetail}}
						</view>
					</view>
					<view class="buttom">
						{{address.name}} {{address.tel}}
					</view>
				</view>
				<view class="right">
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
		</view>
		<view class="productbox" v-for="(item,index) in checkedGoodsList" >
			<view class="left">
				<image :src="item.picUrl" mode=""></image>
			</view>
			<view class="right">
				<view class="navtop">
					{{item.goodsName}}
				</view>
				<view class="navprice">
					<view class="title">
						￥{{item.price}}
					</view>
					<view class="num">
						<text>×{{item.number}}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="footerbtn">
			<view class="pricesum">
				合计{{goodsTotalPrice}}
			</view>
			<view class="pricebtn">
				<u-button type="error" size="normal" text="提交订单" shape="circle" @click="jumporder"></u-button>
			</view>
		</view>
		</view>
</template>

<script>
	export default {
		data() {
			return {
				list:[],
				address: {},
				id:null,//地址id
				checkedGoodsList:[],//获取商品信息
				goodsTotalPrice:0,//商品总价
				carid:0,//购物车id
			}
		},
		methods: {
			addresslist(carid) {
				this.$get(this.API.addresslist).then(res => {
					
					this.list = res.data.data.list
					for (let i = 0; i < res.data.data.limit; i++) {
						if (this.list[i].isDefault === true) {
							this.address = this.list[i]
							if(carid){
								this.getgolist()
							}else{
								this.getcarlist()
							}
							
						}
					}
				})
			},
			jumpadd() {
				let that=this
				uni.navigateTo({
					url: '/pages/address/index/index',
					events: {
						getAddId: function(data) {
							that.id=data.index
						}
					}
				})
			},
			//提交订单
			jumporder(){
				this.$post(this.API.submit,{
					cartId:this.carid!=0?this.carid:0,
					addressId:this.address.id,
					couponId:0,
					userCouponId:0,
					message:this.checkedGoodsList.goodsName,
					grouponRulesId:0,
					grouponLinkId:0
				}).then(res=>{
					if(res.data.errno==0){
						setTimeout(()=>{
							uni.navigateTo({
								url:'/pages/orderpay/orderpay?id='+res.data.data.orderId
							})
						},2000)
						uni.showToast({
							title: res.data.errmsg,
							icon: 'success',
							duration: 2000
						});
					}else{
						uni.showToast({
							title: res.data.errmsg,
							icon: 'error',
							duration: 2000
						});
					}
				})
			},
			//购物车下单
			getcarlist(){
				this.carid=0
				this.$get(this.API.checkout,{
					cartId:0,
					addressId:this.address.id,
					couponId:0,
					userCouponId:0
				}).then(res=>{
					this.checkedGoodsList=res.data.data.checkedGoodsList
					this.goodsTotalPrice=res.data.data.goodsTotalPrice
					console.log(this.checkedGoodsList)
				})
			},
			//立即下单
			getgolist(){
				this.$get(this.API.checkout,{
					cartId:this.carid,
					addressId:this.address.id,
					couponId:0,
					userCouponId:0
				}).then(res=>{
					this.checkedGoodsList=res.data.data.checkedGoodsList
					this.goodsTotalPrice=res.data.data.goodsTotalPrice
					console.log(this.checkedGoodsList)
				})
			}
		},
		onShow() {
			if(this.list==null||this.list==undefined||this.list==''){
				uni.showToast({
					title: '请添加地址',
					icon: 'none',
					duration: 2000
				});
				uni.navigateTo({
					url:'/pages/address/index/index'
				})
			}
		},
		onLoad(options) {
			this.addresslist(options.carid)
			this.carid=options.carid
		},
		//判断是否更新地址
		onShow() {
			if(this.id!=null){
				this.address=this.list[this.id]
			}
		},
		computed:{
			pricesum(){
				return this.productnumber*this.productprice
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		.addressbox {
			padding: 0rpx 20rpx;

			&::after,
			&::before {
				display: table;
				content: '';
				clear: both;
			}

			.address {
				line-height: 7vh;
				border-bottom: 1px solid #007AFF;
			}

			.name {
				line-height: 5vh;
				display: flex;
				border-bottom: 1px solid #007AFF;

				.left {
					width: 90vw;
					display: flex;
					flex-direction: column;

					.buttom {
						font-size: 25rpx;
						color: #959595;
					}
				}

				.right {
					display: flex;
					justify-content: center;
				}
			}
		}
		.productbox{
			display: flex;
			height: 20vh;
			background-color: #f8f8f8;
			
			.left{
				width: 30vw;
				image{
					width: 25vw;
					height: 20vh;
				}
			}
			.right{
				width: 70vw;
				.navtop{
					line-height: 5vh;
				}
				.navprice{
					display: flex;
					align-items: flex-end;
					height: 15vh;
					display: flex;
					
					.title{
						width: 60vw;
						margin-bottom: 20rpx;
					}
					.num{
						margin-bottom: 20rpx;
					}
				}
			}
		}
		.footerbtn{
			width: 100vw;
			position: absolute;
			display: flex;
			line-height: 10vh;
			align-items: center;
			justify-content: space-around;
			bottom: 0;
		}
	}
</style>
